/*
 * utf8mapobject.cpp
 *
 *  Created on: Nov 3, 2009
 *      Author: silviudog
 */

#include "includes/utf8mapobject.h"

namespace templatipus
{

	//constructor && destructor

	TUtf8MapObject::TUtf8MapObject():TMapObject()
	{

	}

	TUtf8MapObject::TUtf8MapObject(const TemplateObject &other):TMapObject(other)
	{
		this->internalMap.clear();
		if (other.getType() == this->getType())
		{
			/*@TODO: what happens to TUnicodeMapObject ???*/
			TUtf8MapObject * otherObj = (TUtf8MapObject*) &other;
			this->internalMap = otherObj->internalMap;
		}
		else
		{
			this->add(MAP_UTF8_DEFAULT_KEY,other);
		}
	}

	TUtf8MapObject::TUtf8MapObject(const int8 &rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const int16 &rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const int32 &rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const int64 &rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const float32 &rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const float64 &rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const std::wstring& rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const std::string& rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const utf8Ptr rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::TUtf8MapObject(const unicodePtr rhs):TMapObject(rhs)
	{
		this->internalMap.clear();
		this->add(MAP_UTF8_DEFAULT_KEY,rhs);
	}

	TUtf8MapObject::~TUtf8MapObject()
	{

	}

	//properties
	TIntNumberObject TUtf8MapObject::getLength() const
	{
		return TIntNumberObject((int64)this->internalMap.size());
	}

	const TemplateObject& TUtf8MapObject::operator +=(const TemplateObject &other)
	{
		if (other.getType() == this->getType())
		{
			/*@TODO: what happens to TUnicodeMapObject ???*/
			TUtf8MapObject * otherObj = (TUtf8MapObject*) &other;
			std::copy(otherObj->internalMap.begin(), otherObj->internalMap.end(), std::inserter(this->internalMap,this->internalMap.begin() ) );
		}
		else
		{
			/*@TODO: we need a unique key ???*/
			//this->add(other);
		}

		return *this;

	}

	TUtf8MapObject TUtf8MapObject::operator +(const TUtf8MapObject &other)
	{
		TUtf8MapObject obj(*this);
		obj+=other;
		return obj;
	}

	//assigning values
	TemplateObject& TUtf8MapObject::operator=(const TemplateObject &other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const int8 &other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const int16 &other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const int32 &other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const int64 &other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const float32 &other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const float64 &other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const std::wstring& other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const std::string& other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const utf8Ptr other)
	{
		return *this;
	}

	TemplateObject& TUtf8MapObject::operator=(const unicodePtr other)
	{
		return *this;
	}

	//comparison
	tbool TUtf8MapObject::operator ==(const TemplateObject &other) const
	{
		return this->equals(other);
	}

	tbool TUtf8MapObject::operator !=(const TemplateObject &other) const
	{
		return !(*this == other);
	}

	const TemplateObject& TUtf8MapObject::operator [](const uint64 index)
	{
		return *this;
	}

	const TemplateObject& TUtf8MapObject::operator [](const utf8Ptr key)
	{
		return *this;
	}

	const TemplateObject& TUtf8MapObject::operator [](const unicodePtr key)
	{
		return *this;
	}

	const TemplateObject& TUtf8MapObject::operator ()(const utf8Ptr key,const TemplateObject &params)
	{
		return *this;
	}

	const TemplateObject& TUtf8MapObject::operator ()(const unicodePtr key,	const TemplateObject &params)
	{
		return *this;
	}

	//String stuff
	TUtf8MapObject::operator std::string() const
	{
		return std::string("");
	}

	TUtf8MapObject::operator std::wstring() const
	{
		return std::wstring(L"");
	}

	//Conversion operators
	//Number
	TUtf8MapObject::operator tbool() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator int8() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator uint8() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator int16() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator uint16() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator int32() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator uint32() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator int64() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator uint64() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator float32() const
	{
		return this->internalMap.size();
	}

	TUtf8MapObject::operator float64() const
	{
		return this->internalMap.size();
	}

	//methods

	void TUtf8MapObject::clear()
	{
		this->internalMap.clear();
	}

	tbool TUtf8MapObject::isEmpty() const
	{
		if(this->internalMap.size())
			return true;

		return false;
	}

	int64 TUtf8MapObject::size() const
	{
		return this->internalMap.size();
	}

	tbool TUtf8MapObject::add(const std::string &key, const TemplateObject & object)
	{
		yasper::ptr<TemplateObject> newPtr = object.newInstance();
		*newPtr = object;

		 std::pair< std::map<std::string , yasper::ptr<TemplateObject> >::iterator,bool> ret;
		 std::pair<std::string , yasper::ptr<TemplateObject> > pair(key,newPtr);

		 ret=this->internalMap.insert (pair);

		 return ret.second;
	}

	void TUtf8MapObject::remove(const std::string &key)
	{
		this->internalMap.erase(key);
	}

	tbool TUtf8MapObject::containsKey(const std::string &key)
	{
		 std::map<std::string , yasper::ptr<TemplateObject> >::iterator it = this->internalMap.find(key);
		 return (it != this->internalMap.end());
	}

	tbool TUtf8MapObject::equals(const TemplateObject &object) const
	{
		if(object.getType() != MAP_TYPE)
			return false;

		TUtf8MapObject* temp = (TUtf8MapObject*)&object;
		return temp->internalMap == this->internalMap;
	}

	std::vector<std::string> TUtf8MapObject::getKeySet()
	{
		std::vector<std::string> keys;
		for(std::map<std::string , yasper::ptr<TemplateObject> >::iterator it = this->internalMap.begin(); it != this->internalMap.end(); ++it)
		{
		  keys.push_back(it->first);
		}

		return keys;
	}

	TemplateObject & TUtf8MapObject::getObject(const std::string & key)
	{
		return *this;
	}

	tbool TUtf8MapObject::containsValue(const TemplateObject & object)
	{
		return false;
	}

	std::vector<TemplateObject> TUtf8MapObject::values()
	{
		std::vector<TemplateObject> values;

		for(std::map<std::string , yasper::ptr<TemplateObject> >::iterator it = this->internalMap.begin(); it != this->internalMap.end(); ++it)
		{
			values.push_back(*(it->second));
		}

		return values;
	}

	void TUtf8MapObject::putAll(const TemplateObject & other)
	{
		if (other.getType() == this->getType())
		{
			/*@TODO: what happens to TUnicodeMapObject ???*/
			TUtf8MapObject * otherObj = (TUtf8MapObject*) &other;
			std::copy(otherObj->internalMap.begin(), otherObj->internalMap.end(), std::inserter(this->internalMap,this->internalMap.begin() ) );
		}
	}

}
